<template>
    <div>
        <img class="bg" src="@/assets/img/bg.jpg">
        <el-form :model="rulesForm" :rules="rules" ref="rulesFormRef" class="login-form">
            <h1 class="h1">校园招聘系统登录</h1>
            <el-form-item label="账号" prop="username">
                <el-input type="text" v-model="rulesForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="rulesForm.password"></el-input>
            </el-form-item>
            <!-- 验证码 -->
            <el-form-item label="验证码" prop="code" >
                <el-input v-model="rulesForm.code"  style="width: 200px;" maxlength="5"></el-input>
                <el-image :src="captchaImg" class="captchaImg" @click="getCaptcha" style="float:right"></el-image>
            </el-form-item>
            <el-form-item >
                <el-radio-group v-model="role">
                    <el-radio :label="1">管理员</el-radio>
                    <el-radio :label="2">用户</el-radio>
                    <el-radio :label="3">企业</el-radio>
                </el-radio-group>
                
            </el-form-item>
            <el-form-item label="">
                <a style="margin-right:10px" href="javascript:void(0)" @click="register('yonghu')">注册用户</a>
                <a style="margin-right:10px" href="javascript:void(0)" @click="register('company')">注册企业信息</a>
            </el-form-item>
            <el-button @click="login()" class="btn-login" type="primary">登录</el-button>
        </el-form>
    </div>
</template>
<script>

    export default {
        data() {
            return {
                rulesForm: {
                    username: "lisi",
                    password: "111111",
                    code:'',
                    token:'',
                },
                label:'',
                menus: [],
                tableName: "",
                role:'',
                rules: {
                    username: [{required: true, message: "请输入账号", trigger: "blur"}],
                    password: [{required: true, message: "请输入密码", trigger: "blur"}],
                    role: [{required: true, message: "请选择角色", trigger: "blur"}]
                },
                user:'',
                captchaImg: null
            };
        },
        created(){
            this.getCaptcha()
        },
        methods: {
            register(tableName) {
                window.sessionStorage.setItem('tableName', tableName)
                this.$router.push('/Register')
            },
            // 登录
            login() {
                this.$refs.rulesFormRef.validate(async(valid)=>{
                    if(!valid) return
                    const {data:res}=await this.$http.post('login?code='+this.rulesForm.code+"&&token="+this.rulesForm.token,this.rulesForm)
                    if(res.meta.status!=200) return this.$message.error(res.meta.msg)
                    window.sessionStorage.setItem("token",res.data.token)
                    this.$store.dispatch('asyncUpdateUser',{username:this.rulesForm.username})

                    const {data:user}=await this.$http.get('user?username='+this.rulesForm.username)
                    this.user=user.data
                    let flag=false
                    //用户登录
                    if(this.role=='2'){
                        if(this.user.roleid==2){
                            flag=true
                            this.$router.push('/index')
                        }
                    }
                    //企业登录
                    if(this.role=='3'){
                        if(this.user.roleid==3){
                            flag=true
                            this.$router.push('/ComHome')
                        }
                    }
                    //管理员登录
                    if(this.role=='1'){
                        if(this.user.roleid!=3&&this.user.roleid!=2){
                            flag=true
                            this.$router.push('/Home')
                        }
                    }
                    if(!flag){
                        return this.$message.error("登录失败，请选择正确的角色")
                    }else{
                        window.sessionStorage.setItem("user",JSON.stringify(this.user))
                        return this.$message.success("登录成功")
                    }
                })
            },
             async getCaptcha() {
				await this.$http.get('/captcha').then(res => {
					this.rulesForm.token = res.data.data.token
					this.captchaImg = res.data.data.captchaImg
                    this.rulesForm.code = ''
				})
		    }
        }
    };
</script>
<style lang="scss" scoped>
</style>